package com.example.treemap_array_linkedlist;

import java.util.Arrays;
import java.util.Random;

public class ArrayPerformanceTest {

    public static void main(String[] args) {
        // 数据规模
        int dataSize = 1_000_000;
        // 创建数组
        int[] array = new int[dataSize];
        // 随机数生成器
        Random random = new Random();

        // 生成随机数据并插入
        long startTime = System.nanoTime();
        for (int i = 0; i < dataSize; i++) {
            array[i] = random.nextInt(); // 随机生成数据并插入数组
        }
        long insertTime = System.nanoTime() - startTime;

        // 随机查找1000次，计算查找时间
        startTime = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            int randomIndex = random.nextInt(dataSize); // 随机生成索引
            for(int num : array){
                if(num == randomIndex){
                    break;
                }
            }
        }
        long searchTime = System.nanoTime() - startTime;

        // 随机删除1000次，计算删除时间
        startTime = System.nanoTime();
        for (int i = 0; i < 1000; i++) {
            int randomIndex = random.nextInt(dataSize); // 随机生成索引
            // 删除元素，通过将要删除元素后面的元素向前移动来模拟删除操作
            for (int j = randomIndex; j < dataSize - 1; j++) {
                array[j] = array[j + 1];
            }
            dataSize--; // 数组大小减1
        }
        long deleteTime = System.nanoTime() - startTime;

        // 打印时间
        System.out.println("Insertion time: " + insertTime / 1000000.0 + " ms");
        System.out.println("Search time: " + searchTime / 1000000.0 + " ms");
        System.out.println("Deletion time: " + deleteTime / 1000000.0 + " ms");
    }
}
